package com.WISSC.serviceImpl;

import com.WISSC.bean.*;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.bean.layuiBean.VoPage;
import com.WISSC.mapper.TeamMapper;
import com.WISSC.service.TeamMatchService;
import com.WISSC.service.TeamService;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 战队信息表 服务实现类
 * </p>
 *
 * @author 张凯
 * @since 2019-05-06
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {

    @Autowired
    TeamMatchService teamMatchService;

    @Override

    public ResultPage<Team> findPage(int current, int size, String teamName, String city, Long buildingTime, Integer teamLevel,HttpSession session,boolean admin) {

        Map paramMap = new HashMap<>();
        paramMap.put("page", (current - 1) * size);
        paramMap.put("limit", size);
        paramMap.put("teamName", teamName);
        paramMap.put("city", city);
        System.out.println("city:" + city);
        paramMap.put("buildingTime", buildingTime);
        paramMap.put("teamLevel", teamLevel);
        List<Team> list = baseMapper.selectTeam(paramMap,admin);
        Integer count = baseMapper.countTeam(paramMap,admin);
        ResultPage<Team> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @return 战队赛事登陆
     */
    @Override
    public List<Team> login(String teamName) {
        Map map = new HashMap<>();
        map.put("teamName", teamName);
        List list = baseMapper.login(map);
        return list;
    }

    /**
     * @param session
     * @return 战队赛事信息查询
     */
    @Override
    public List<Team> selectByName(HttpSession session) {
        String teamName = ((Team) session.getAttribute("teamName")).getTeamName();
        //战队积分
        List<Team> list = new ArrayList<Team>();
        list = baseMapper.selectByName(teamName);
        int b1 = 0;
        for (int i = 0; i < list.size(); i++) {
            b1 = b1 + list.get(i).getIntegral();
        }
        list.get(0).setTeamIntegral(b1);
        //队龄
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long l = list.get(0).getBuildingTime();
        String[] strNow = sdf.format(new Date()).toString().split("-");
        String year = strNow[0];
        String[] strNow1 = sdf.format(l).toString().split("_");
        String year1 = strNow[0];
        Long a = Long.valueOf(year) - Long.valueOf(year1);
        String age = String.valueOf(a) + " 年";
        list.get(0).setAge(age);
        return list;
    }

    /**
     * @param team
     * @return 添加战队信息
     */
    @Override
    public String teamInsert(String param2, Team team) {
        Map map = new HashMap();
        //添加主表 + 次表
        if (param2 != "") {
            String[] str1 = param2.split("-");
            ArrayList list = new ArrayList<>();
            for (int i = 0; i < str1.length; i++) {
                list.add(str1[i]);
            }
            //个人信息主表 --创建日期
            team.setCreateTime(DateUtil.date());
            //个人信息 --添加
            if (this.save(team)) {
                for (int i = 0; i < list.size(); i++) {
                    String str = (String) list.get(i);
                    String[] arr = str.split(",");
                    if (arr != null) {
                        for (int j = 0; j < 1; j++) {
                            if (arr[0].equals("NaN")) {
                                break;
                            }
                            TeamMatch teamMatch = new TeamMatch();
                            teamMatch.setMatchTime(Long.parseLong(arr[0]));
                            teamMatch.setMatchName(arr[1]);
                            teamMatch.setRanking(Integer.parseInt(arr[2]));
                            teamMatch.setIntegral(Integer.parseInt(arr[3]));
                            teamMatch.setTeamId(team.getId());
                            teamMatch.setCreateTime(DateUtil.date());
                            teamMatchService.save(teamMatch);
                        }
                    } else {
                        map.put("flag", true);
                        map.put("msg", "添加成功");
                        return JSON.toJSONString(map);
                    }
                }
            }else {
                map.put("flag", false);
                map.put("msg", "添加失败");
                return JSON.toJSONString(map);
            }
        } else {
            // 添加主表
            if (!this.save(team)) {
                map.put("flag", false);
                map.put("msg", "添加失败");
                return JSON.toJSONString(map);
            }
        }
        map.put("flag", true);
        map.put("msg", "添加成功");
        return JSON.toJSONString(map);
    }

   /* @Override
    public ResultPage<Team> selectMyPage(String teamName, VoPage voPage) {
        *//*String teamName = ((Team) session.getAttribute("teamName")).getTeamName();
        String city = ((Team) session.getAttribute("teamName")).getCity();*//*
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("teamName",teamName);
        List<Team> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Team> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }*/

    /**
     * @param
     * @param
     * @param
     * @return 战队信息查询
     */
    @Override
    public Team select(String teamName, String city) {
       /* System.out.println("城市:"+city);
        String[] str = city.split(",");
        for(int i=0;i<str.length;i++){
            //System.out.println(str[i].toString());





        }*/
        return baseMapper.select(teamName, city);
    }

    /**
     * @param city
     * @param teamName
     * @return 排名
     */
    @Override
    public List<Team> country(String city, String teamName) {
        String[] str = city.split("/");
        List list = new ArrayList();
        for (int i = 0; i < str.length; i++) {
            if (str[0] != null) {
                list.add(baseMapper.country(str[0], teamName));
            }
            if (str[1] != null) {
                list.add(baseMapper.country(str[1], teamName));
            }
            if (str[2] != null) {
                list.add(baseMapper.country(str[2], teamName));
            }
        }
        return list;
    }

    /**
     * @param id
     * @return 战队编辑回显city
     */
    @Override
    public Map getTeamCity(Long id) {
        String str = "";
        String sum = "";
        Map map = new HashMap();
        List<Team> list = baseMapper.getTeamCity(id);
        for (int i = 0; i < list.size(); i++) {
            str = list.get(i).getCity();
        }
        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getMatchName()!=null){
                String res= "";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                res = simpleDateFormat.format(list.get(i).getMatchTime());
                sum += list.get(i).getTeamId() + "," + res + "," + list.get(i).getMatchName() + "," + list.get(i).getRanking() + "," + list.get(i).getIntegral()  + "=";
            }
        }
        map.put("str", str);
        map.put("sum", sum);
//        String sum = str +"="+arr;
//        System.out.println("sum"+sum);
        return map;
    }

    /**
     * @param teamName
     * @return 战队查询
     */
    @Override
    public ResultPage<Team> selectTeamName(VoPage voPage, String teamName) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("teamName", teamName);
        List<Team> list = baseMapper.selectTeamName(paramMap);
        Integer count = baseMapper.countTeamName(paramMap);
        ResultPage<Team> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public Map updateTeam(Team team, String param2) {
        if (this.updateById(team)) {
            String[] split = param2.split("-");
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split(",");
                for (int j = 0; j < 1; j++) {
                    if (split[0].equals("undefined") || split[0] == "") {
                        break;
                    }
                    TeamMatch teamMatch = new TeamMatch();
                    teamMatch.setId(Long.parseLong(split1[0]));
                    teamMatch.setMatchTime(Long.parseLong(split1[1]));
                    teamMatch.setMatchName(split1[2]);
                    teamMatch.setRanking(Integer.parseInt(split1[3]));
                    teamMatch.setIntegral(Integer.parseInt(split1[4]));
                    if (!teamMatchService.updateById(teamMatch)) {
                        return ResponseUtil.getExceptionMap();
                    }
                }
            }
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    @Override
    public List<Team> getTeam(String teamName) {
        return baseMapper.getTeam(teamName);
    }


}
